﻿using System;
using System.Collections.Generic;

namespace OperationPlugins.Validators
{
    public class SimpleRuleChecker
    {
        protected IList<string> Messages { get; set; }

        protected bool PassConditionResult { get; set; }

        public SimpleRuleChecker(IList<string> messages, bool passConditionResult)
        {
            if (messages == null)
            {
                throw new ArgumentNullException("messages");
            }

            Messages = messages;
            PassConditionResult = passConditionResult;
        }

        protected virtual void Verify(bool result, string message, params object[] parameters)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (result != PassConditionResult)
            {
                lock (Messages)
                {
                    Messages.Add(FormatMessage(message, parameters));
                }
            }
        }

        protected virtual string FormatMessage(string message, params object[] parameters)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            string s = parameters.Length == 0 ? message : string.Format(message, parameters);
            return s;
        }

        /// <summary>
        /// Verifies that the specified condition is true. The check fails if the condition is false.
        /// </summary>
        /// <param name="condition">The condition to verify is true.</param>
        /// <param name="message">A message to raise if the rule check fails.</param>
        /// <param name="parameters">An array of parameters to use when formatting message.</param>
        public virtual void True(bool condition, string message, params object[] parameters)
        {
            Verify(condition == true, message, parameters);                       
        }

        /// <summary>
        /// Verifies that the specified condition is false. The check fails if the condition is true.
        /// </summary>
        /// <param name="condition">The condition to verify is false.</param>
        /// <param name="message">A message to raise if the rule check fails.</param>
        /// <param name="parameters">An array of parameters to use when formatting message.</param>
        public virtual void False(bool condition, string message, params object[] parameters)
        {
            Verify(condition == false, message, parameters);
        }
    }
}
